﻿using Digitalmes.Domain.Entities.Mes.Masters;

namespace Digitalmes.Application.Mes.Masters.Workstations.Commands;

/// <summary>
/// 创建工位命令。
/// </summary>
/// <param name="Workstation"></param>
public record CreateWorkstationCommand(MdWorkstationDto Workstation) : IRequest<IResult<bool>>;

internal sealed class CreateWorkstationCommandHandler(IRepository<MdWorkstation> repo) : IRequestHandler<CreateWorkstationCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(CreateWorkstationCommand request, CancellationToken cancellationToken)
    {
        if (await repo.IsAnyAsync(m => m.LineId == request.Workstation.LineId && m.WsCode == request.Workstation.WsCode, cancellationToken))
        {
            return Result.Failure("工位编号已存在");
        }

        var ws = request.Workstation.Adapt<MdWorkstation>();
        var ok = await repo.InsertAsync(ws, cancellationToken);
        return Result.From(ok);
    }
}
